package modelimplementation

import (
	"math"
	"salary/model"
	"time"
)

/*
 * 按工作小时支付
 */

type HourlyClassification struct {
	rate			float64
	timeCards 		map[time.Time]TimeCard
}

func NewHourlyClassification(rate float64) *HourlyClassification {
	return &HourlyClassification{rate: rate}
}

func (hc *HourlyClassification) GetRate() float64 {
	return hc.rate
}

func (hc *HourlyClassification) GetTimeCard(date time.Time) TimeCard {
	return hc.timeCards[date]
}

func (hc *HourlyClassification) AddTimeCard(timeCard TimeCard) {
	hc.timeCards[timeCard.GetDate()] = timeCard
}

// 按照时间卡算薪水

func (hc *HourlyClassification) CalculatePayForTimeCard(timeCard TimeCard) float64 {
	hours := timeCard.GetHours()
	overtime := math.Max(0, hours-8)
	straightTime := hours - overtime
	return straightTime*hc.rate + overtime*hc.rate*1.5
}

// 按工作小时支付计算薪水

func (hc *HourlyClassification) CalculatePay(pc model.PayCheck) float64 {
	totalPay := 0.0
	for date := range hc.timeCards {
		timCard := hc.timeCards[date]
		date1 := pc.GetPayPeriodStartDate()
		date2 := pc.GetPayPeriodEndDate()

		if date.After(date1) && date.Before(date2) {
			totalPay += hc.CalculatePayForTimeCard(timCard)
		}
	}

	return totalPay
}
